package com.windfone.web.action;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.StringUtil;
import com.windfone.model.Askreponse;
import com.windfone.model.Color;
import com.windfone.model.DetailMsg;
import com.windfone.model.PackageCompose;
import com.windfone.model.Product;
import com.windfone.service.AskReponseService;
import com.windfone.service.CommonService;
import com.windfone.service.ProductService;

public class DetailMobileAction extends ActionSupport {

	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private CommonService commonService;
	private List<Color> colors;
	private Product mobile;
	private String productId ;
	private List<PackageCompose> packages;
	private DetailMsg detailMsg;
	private List<Askreponse>  askReponses;
	private AskReponseService askReponseService;
	private List<Product> recommendProducts;
 
 
	public List<Product> getRecommendProducts() {
		return recommendProducts;
	}
	public void setRecommendProducts(List<Product> recommendProducts) {
		this.recommendProducts = recommendProducts;
	}
	public AskReponseService getAskReponseService() {
		return askReponseService;
	}
	public void setAskReponseService(AskReponseService askReponseService) {
		this.askReponseService = askReponseService;
	}
	public List<Askreponse> getAskReponses() {
		return askReponses;
	}
	public void setAskReponses(List<Askreponse> askReponses) {
		this.askReponses = askReponses;
	}
	public DetailMsg getDetailMsg() {
		return detailMsg;
	}
	public void setDetailMsg(DetailMsg detailMsg) {
		this.detailMsg = detailMsg;
	}
	public List<PackageCompose> getPackages() {
		return packages;
	}
	public void setPackages(List<PackageCompose> packages) {
		this.packages = packages;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public List<Color> getColors() {
		return colors;
	}
	public void setColors(List<Color> colors) {
		this.colors = colors;
	
	}
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	@Override
	public String execute() throws Exception {
		if ( productId != null &&  !"".equals( productId )){
			if ( StringUtil.isInteger(  productId )){
				List<Product> ps=	productService.getProductById( Integer.parseInt(  productId ) );
				if ( ps.size()>0) {
					mobile = ps.get(0);
					colors = commonService.getColorByProductId(Integer.parseInt(  productId ));
					packages = commonService.getPackageByProductId(Integer.parseInt(  productId ));
					detailMsg =commonService.getDetailMsgByProductId(Integer.parseInt(  productId ));
					 	
					ServletContext application = ServletActionContext.getServletContext();  
			        WebApplicationContext wac = WebApplicationContextUtils       .getWebApplicationContext(application);
			        if ( askReponseService == null ) 
			        	askReponseService  = (AskReponseService )wac.getBean("askReponseService");
					askReponses = askReponseService.getAskReponseByProductAndType(Integer.parseInt(  productId ), 1);
					recommendProducts = productService.searchByReference(mobile.getProductTypeId(),mobile.getPrice()  );
				}
			}
		}
		return SUCCESS;
	}
	public Product getMobile() {
		return mobile;
	}
	public void setMobile(Product mobile) {
		this.mobile = mobile;
	}

}
